package test.bean;

import static javax.persistence.GenerationType.IDENTITY;

import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderBy;

/**
 * 用户
 * @ManyToMany user#id=UserRole#userId~UserRole#roleId=Role#id#name
 * @author wangjg
 */
@Entity
public class User implements java.io.Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = IDENTITY)
	private Long id; //id
	
	private String username;//用户名

	private String password;//密码
	
	@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE },fetch=FetchType.EAGER)  
    @JoinTable(name = "USER_ROLE", joinColumns = { @JoinColumn(name = "USERID") }, inverseJoinColumns = { @JoinColumn(name = "ROLEID") })  
    @OrderBy("id")
	private Set<Object> roles = new LinkedHashSet<Object>();
	
	//@ManyToMany User#id=UserRole#userId~UserRole#roleId=Role#id#name
	private String roleIds;

	public User() {
	}
	
	public Set<Object> getAuthorities() {
		return roles;
	}
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	public Set<Object> getRoles() {
		return roles;
	}
	public void setRoles(Set<Object> roles) {
		this.roles = roles;
	}
	@Override
	public String toString() {
		return "\nUser [id=" + id + ", username=" + username + ", password="
				+ password + "]";
	}
}
